﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp0
{
    public partial class shang_ku0 : Form
    {
        public shang_ku0()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        private void Table_pin()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Merchandise";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d, e;
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                e = dr["Mpurchas"].ToString();

                string[] str = { a, b, c, d, e };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();

        }
        private void Table_cu()
        {
            dataGridView2.Rows.Clear();
            string sql = "select * from Depot";

            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b="", c="", d="", e, f;
                a = dr["Mid"].ToString();
                

                string sql1 = $"select * from Merchandise where Mid='{a}'";
                IDataReader dr1 = dao.read(sql1);
                while (dr1.Read())
                {
                    b = dr1["Mname"].ToString();
                    c = dr1["Munit"].ToString();
                    d = dr1["Mnorm"].ToString();
                }
                dr1.Close();
                e = dr["Iplace"].ToString();
                f = dr["Dstock"].ToString();
                  
                string[] str = { a, b, c, d, e, f };
                dataGridView2.Rows.Add(str);
            }
            dr.Close();
            
        }
        private void shang_ku0_Load(object sender, EventArgs e)
        {
            Table_pin();
            Table_cu();
        }
        private void name_search()
        {//查名 显示
            dataGridView2.Rows.Clear();
            string sql = $"select * from Merchandise where Mname like'%{textBox1.Text}%'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b = "", c = "", d = "", e = "", f = "";
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                string sql1 = $"select * from Depot where Mid='{a}'";
                IDataReader dr1 = dao.read(sql1);
                while (dr1.Read())
                {
                        e = dr1["Iplace"].ToString();
                        f = dr1["Dstock"].ToString();
                }
                dr1.Close();
                string[] str = { a, b, c, d, e, f };
                dataGridView2.Rows.Add(str);
            }
            dr.Close();
           // Table_cu();
        }
        private void name_search_pin()
        {//查名 显示
            dataGridView1.Rows.Clear();
            string sql = $"select * from Merchandise where Mname like'%{textBox2.Text}%'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b = "", c = "", d = "", e = "", f = "";
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                e= dr["Mpurchas"].ToString();
                string[] str = { a, b, c, d, e };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();

        }
        private void name_search_cun()
        {//查名 显示
            dataGridView2.Rows.Clear();
            string sql = $"select m.Mid,m.Mname,m.Munit,m.Mnorm,d.Iplace,d.Dstock from Merchandise m  join Depot d on m.Mid=d.Mid where m.Mname like '%{textBox1.Text}%'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b = "", c = "", d = "", e = "", f = "";
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                e = dr["Iplace"].ToString();
                f = dr["Dstock"].ToString();
                string[] str = { a, b, c, d, e ,f};
                dataGridView2.Rows.Add(str);
            }
            dr.Close();

        }
        private void button5_Click(object sender, EventArgs e)
        {
            name_search_pin();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string mid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                ku_ru ruku = new ku_ru(mid);
                MessageBox.Show("商品编号:"+mid);
                ruku.ShowDialog();
              
                //入库 刷新
            }
            
            catch
            {
                MessageBox.Show("错误");
            }                                                                                       
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Table_cu();
        
        }

        private void button2_Click(object sender, EventArgs e)
        {//商品名称 查询 确认按钮
            name_search_cun();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Table_cu();
            Table_pin();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Table_pin();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            string mid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = $"select m.Mid,m.Mname,m.Munit,m.Mnorm,d.Iplace,d.Dstock from Merchandise m  join Depot d on m.Mid=d.Mid where m.Mid = '{mid}'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b = "", c = "", d = "", p= "", f = "";
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                p = dr["Iplace"].ToString();
                f = dr["Dstock"].ToString();
                string[] str = { a, b, c, d, p, f };
                dataGridView2.Rows.Add(str);
            }
            dr.Close();
        }
    }
}
